﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
namespace CRMSystemV2.MVCWeb.Controllers
{
    public abstract class BaseController : Controller
    {
        protected readonly CRMSystemAPI.CRMSystemAPIClient api = new CRMSystemAPI.CRMSystemAPIClient();

        /// <summary>
        /// 分页信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected int rowTotal = 0;
        protected int pageTotal = 0;
        /// <summary>
        /// 搜索关键字
        /// </summary>
        protected static Dictionary<string, string> keyWords = new Dictionary<string, string>();
        /// <summary>
        /// 排序
        /// </summary>
        protected string orderField = "";
        protected string orderDirection = "";
        //
        // GET: /Base/

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            if (requestContext.HttpContext.Session["username"] == null)
            {
                requestContext.HttpContext.Session["username"] = "admin";
                //RedirectToAction("Login", "Account");
            }
            else
            {
                //分页信息,该方法不能解决翻页定位问题
                pageIndex = requestContext.HttpContext.Request["pageNum"] == null ? 1 : int.Parse(requestContext.HttpContext.Request["pageNum"]);
                pageSize = requestContext.HttpContext.Request["numPerPage"] == null ? int.Parse(ConfigurationManager.AppSettings["pageSize"]) : int.Parse(requestContext.HttpContext.Request["numPerPage"]);
                orderField = requestContext.HttpContext.Request["orderField"] == null ? "" : requestContext.HttpContext.Request["orderField"];
                orderDirection = requestContext.HttpContext.Request["orderDirection"] == null ? "" : requestContext.HttpContext.Request["orderDirection"];
            }
            base.Initialize(requestContext);
        }
        /// <summary>
        /// 获取搜索关键字的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected static string GetValue(string key)
        {
            if (!keyWords.ContainsKey(key))
                return "";
            return keyWords[key];
        }
        /// <summary>
        /// 保存搜索关键字的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        protected static void SetValue(string key, string value)
        {
            if (!keyWords.ContainsKey(key))
                keyWords.Add(key, value);
            else
                keyWords[key] = value;
        }        
    }
}
